package com.example.elmspringboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.elmspringboot.controller.utils.Result;
import com.example.elmspringboot.domain.Business;
import com.example.elmspringboot.domain.Food;
import com.example.elmspringboot.service.FoodService;
import com.example.elmspringboot.service.Impl.FoodServiceImpl;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Tag(name = "FoodController", description = "菜单管理")
@RestController//@ResponseBody @Controller
@RequestMapping("/api/food")
public class FoodController {
    @Resource
    private FoodService foodService;
// 获得所有食品
    @GetMapping
    public Result getFoods(@RequestParam Integer shopId){
        List<Food> foods = foodService.list(new QueryWrapper<Food>().eq("shop_id", shopId));
        return new Result(true, foods);
    }
//    获得上架的食品
    @GetMapping("/get-pass-food")
    public Result getPass(@RequestParam Integer shopId){
        List<Food> foods = foodService.list(new QueryWrapper<Food>().eq("shop_id", shopId).eq("status", 1));
        return new Result(true, foods);
    }
//    保存食品
    @PostMapping
    public Result saveFood(@RequestBody Food food){
        Boolean flag=foodService.save(food);
        String msg=flag ?"success":"fail";
        return  new Result(flag,null,msg);
    }
    @DeleteMapping("/{id}")
    public Result deleteFoodById(@PathVariable Integer id){
        Boolean flag=foodService.removeById(id);
        String msg=flag? "success":"fail";
        return new Result(flag,null,msg);
    }
//    上架食品
    @PutMapping
    public Result updateFood(@RequestParam Integer id){
        Food food = foodService.getById(id);
        food.setStatus(1);
        foodService.updateById(food);
        return new Result(true, null);
    }
    @GetMapping("/{currentPage}/{pageSize}")
    public Result getPage(@PathVariable Integer currentPage, @PathVariable Integer pageSize , Food food){
        IPage<Food> page = foodService.getPage(currentPage, pageSize,food);
        if(currentPage>page.getPages()){
            page=foodService.getPage((int)page.getPages(),pageSize,food);
        }
        return new Result(true,page);
    }

}
